/*
Copyright 2010,2011 Matt Van Der Westhuizen

This file is part of CGLL.

CGLL is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with CGLL.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.chaoticengine.cgll.twl;

import de.matthiasmann.twl.ActionMap;
import de.matthiasmann.twl.DesktopArea;
import de.matthiasmann.twl.Event;
import de.matthiasmann.twl.Widget;
import org.chaoticengine.cgll.CGLLGameState;

/**
 *
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
public class RootPane extends DesktopArea {
    protected CGLLGameState state = null;
    protected int oldMouseX = 0;
    protected int oldMouseY = 0;

    public RootPane(CGLLGameState state) {
        if (state == null) {
            throw new NullPointerException("state");
        }
        this.state = state;

        ActionMap actionMap = getActionMap();
        if (actionMap == null) {
            actionMap = new ActionMap();
            setActionMap(actionMap);
        }

        actionMap.addMapping(state);
        setCanAcceptKeyboardFocus(true);

        setTheme("");
    }

    @Override
    protected void keyboardFocusLost() {
        state.keyboardFocusLost();
    }

    @Override
    protected boolean requestKeyboardFocus(Widget child) {
        if (child != null) {
            state.keyboardFocusLost();
        }
        return super.requestKeyboardFocus(child);
    }


    @Override
    protected boolean handleEvent(Event evt) {
        if (super.handleEvent(evt)) {
            return(true);
        }

        switch (evt.getType()) {
            case KEY_PRESSED:
                state.keyPressed(evt.getKeyCode(), evt.getKeyChar());
                break;
            case KEY_RELEASED:
                state.keyReleased(evt.getKeyCode(), evt.getKeyChar());
                break;
            case MOUSE_BTNDOWN:
                state.mousePressed(evt.getMouseButton(), evt.getMouseX(), evt.getMouseY());
                break;
            case MOUSE_BTNUP:
                state.mouseReleased(evt.getMouseButton(), evt.getMouseX(), evt.getMouseY());
                break;
            case MOUSE_CLICKED:
                state.mouseClicked(evt.getMouseButton(), evt.getMouseX(), evt.getMouseY(), evt.getMouseClickCount());
                break;
            case MOUSE_ENTERED:
            case MOUSE_MOVED:
                state.mouseMoved(oldMouseX, oldMouseY, evt.getMouseX(), evt.getMouseY());
                break;
            case MOUSE_DRAGGED:
                state.mouseDragged(oldMouseX, oldMouseY, evt.getMouseX(), evt.getMouseY());
                break;
            case MOUSE_WHEEL:
                state.mouseWheelMoved(evt.getMouseWheelDelta());
                break;
        }

        if (evt.isMouseEvent()) {
            oldMouseX = evt.getMouseX();
            oldMouseY = evt.getMouseY();
        }

        return true;
    }

    @Override
    protected void layout() {
        super.layout();
        state.layoutRootPane();
    }
}
